package com.android.ailaoshan.index.listener;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.camera.StartCamera;
import com.android.ailaoshan.index.MainActivity;
import com.android.ailaoshan.task.TaskActivity;
import com.android.ailaoshan.voice.StartVoice;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;

public class ButtonClickListener implements View.OnClickListener {

    private MapView mMapView;
    private Context mContext;
    private MainActivity mainActivity;

    public ButtonClickListener(MapView mMapView, Context mContext,MainActivity mainActivity) {
        this.mMapView = mMapView;
        this.mContext = mContext;
        this.mainActivity = mainActivity;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            // 定位按钮
            case R.id.floatingActionButton:{
                showDeviceLocation();
                break;
            }
            // 拍照按钮
            case R.id.FloatingToTakePhoto:{
                new StartCamera(mContext,mainActivity);
                break;
            }
            // 录音按钮
            case R.id.takeVoice:{
                new StartVoice(mContext,mainActivity);
                break;
            }
            // 任务按钮
            case R.id.my_job:{
                Intent taskIntent = new Intent(mContext, TaskActivity.class);
                mContext.startActivity(taskIntent);
                break;
            }
            // 地图放大
            case R.id.mapview_zoom_in:{
                double mapScale = this.mMapView.getMapScale();
                if(mapScale*0.5 <= 2000.0){
                    this.mMapView.setViewpointScaleAsync(2000.0);
                }else{
                    this.mMapView.setViewpointScaleAsync(mapScale*0.5);
                }
                break;
            }
            // 地图缩小
            case R.id.mapview_zoom_out:{
                double mapScale = mMapView.getMapScale();
                if(mapScale*2 >= 10000000.0){
                    mMapView.setViewpointScaleAsync(10000000.0);
                }else{
                    mMapView.setViewpointScaleAsync(mapScale*2);
                }
                break;
            }
        }
    }


    /**
     * 显示设备位置
     */
    private void showDeviceLocation(){
        if(this.mMapView != null){
            LocationDisplay locationDisplay = this.mMapView.getLocationDisplay();
            // 位置监听
            locationDisplay.addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
                @Override
                public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {

                    if (dataSourceStatusChangedEvent.isStarted() || dataSourceStatusChangedEvent.getError() == null) {
                        return;
                    }else{
                        String message = String.format("出现错误: %s",
                                dataSourceStatusChangedEvent.getSource().getLocationDataSource().getError().getMessage());
                        Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
                    }
                }
            });
            locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.COMPASS_NAVIGATION);
            locationDisplay.startAsync();
        }
    }
}
